#include"global_var.cpp"
#include"read_netlist.cpp"
#include"topological_ckt.cpp"
#include"get_pat.cpp"
#include"thread_pool.cpp"
#include"write_result.cpp"

int main(int argc,char** argv)
{
  auto start_time = std::chrono::high_resolution_clock::now();

  std::string ckt_name = argv[1];

  std::string netlist_path  = ckt_name+".v";
  std::string patterns_path = ckt_name+".vcd";
  std::string fault_list_path = ckt_name+".fl";

  std::string atpg_lib_path = argv[2];

  read_atpg_lib(atpg_lib_path);

  read_netlist(netlist_path);
  get_pat(patterns_path);

  int thread_num = 4;
  int packet_task = 1000;

  result res(ckt_name);

  read_faults(fault_list_path, res);

  ThreadPool* tp = new ThreadPool(thread_num);

  for(int i=0,sz=collapsed_fault.size() ; i<sz ; i+=packet_task)
  {
    std::vector<fault*> tmp_vec(packet_task+1,nullptr);
    tmp_vec[0] = new fault;
    for(int j=1 ; j+i-1<sz && j<=packet_task ; ++j)
      tmp_vec[j] = collapsed_fault[j+i-1];

    while(tmp_vec.back()==nullptr) tmp_vec.pop_back();
    tp->enqueue(tmp_vec,&res);
  }

  delete tp;

  auto end_time = std::chrono::high_resolution_clock::now();
  std::cout<<std::chrono::duration<double>(end_time-start_time).count()<<std::endl;

  return 0;
}
